using UnityEngine;

/// <summary>
/// 游戏管理器，使用单例模式
/// </summary>
public class GameManager : Singleton<GameManager>
{
    private StateMachine _stateMachine;

    // 游戏状态枚举
    public enum GameState
    {
        MainMenu,
        Playing,
        Paused,
        GameOver
    }

    protected override void Awake()
    {
        base.Awake(); // 调用基类的Awake方法
        InitializeStateMachine();
    }

    private void InitializeStateMachine()
    {
        _stateMachine = new StateMachine();

        // 添加各种游戏状态
        _stateMachine.AddState(GameState.MainMenu.ToString(), new MainMenuState());
        _stateMachine.AddState(GameState.Playing.ToString(), new PlayingState());
        _stateMachine.AddState(GameState.Paused.ToString(), new PausedState());
        _stateMachine.AddState(GameState.GameOver.ToString(), new GameOverState());

        // 设置初始状态
        _stateMachine.ChangeState(GameState.MainMenu.ToString());
    }

    private void Update()
    {
        _stateMachine.Update();
    }

    /// <summary>
    /// 切换游戏状态
    /// </summary>
    /// <param name="state">目标状态</param>
    public void ChangeState(GameState state)
    {
        _stateMachine.ChangeState(state.ToString());
    }

    /// <summary>
    /// 获取当前游戏状态
    /// </summary>
    /// <returns>当前游戏状态</returns>
    public GameState GetCurrentGameState()
    {
        var state = _stateMachine.GetCurrentState();
        if (state is MainMenuState)
            return GameState.MainMenu;
        else if (state is PlayingState)
            return GameState.Playing;
        else if (state is PausedState)
            return GameState.Paused;
        else if (state is GameOverState)
            return GameState.GameOver;
        else
            return GameState.MainMenu;
    }
}